/*!
\copyright  Copyright (c) 2020-2021 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file
\brief      LE advertising data required for the volume renderer role of VCS.
*/

#include "volume_renderer_role_advertising.h"

#include "le_advertising_manager.h"

#include <logging.h>

#include <panic.h>
#include <stdlib.h>


#define VOLUME_CONTROL_SERVER_NUMBER_ADVERT_DATA_ITEMS 	1
#define VOLUME_CONTROL_SERVER_SIZE_ADVERT              	4

#define VOLUME_CONTROL_SERVICE_UUID                     0x1844

static le_adv_item_handle volume_renderer_registered_handle = NULL;
static le_adv_item_data_t volume_renderer_advert;

static const uint8 volume_renderer_data[VOLUME_CONTROL_SERVER_SIZE_ADVERT] = {
    VOLUME_CONTROL_SERVER_SIZE_ADVERT - 1,
    ble_ad_type_service_data,
    VOLUME_CONTROL_SERVICE_UUID & 0xFF,
    VOLUME_CONTROL_SERVICE_UUID >> 8
};

static bool volumeRenderer_GetItemData(le_adv_item_data_t * item)
{
    PanicNull(item);
    *item = volume_renderer_advert;
    return TRUE;
}

#ifndef INCLUDE_LEGACY_LE_ADVERTISING_MANAGER

static bool volumeRenderer_GetItemInfo(le_adv_item_info_t * info)
{
    PanicNull(info);
    *info = (le_adv_item_info_t){ .placement = le_adv_item_data_placement_advert,
                                    .type = le_adv_type_extended_connectable,
                                    .data_size = VOLUME_CONTROL_SERVER_SIZE_ADVERT };
    return TRUE;
}

le_adv_item_callback_t volumeRenderer_AdvertisingManagerCallback =
{
    .GetItemData = &volumeRenderer_GetItemData,
    .GetItemInfo = &volumeRenderer_GetItemInfo
};

#else

static bool volumeRenderer_CanAdvertise(const le_adv_data_params_t * params)
{
    bool can_advertise = FALSE;
    if(params->data_set == le_adv_data_set_extended_handset
        && params->completeness == le_adv_data_completeness_full
        && params->placement == le_adv_data_placement_advert)
    {
        can_advertise = TRUE;
    }
    return can_advertise;
}

static unsigned int volumeRenderer_GetNumberOfAdvertisingItems(const le_adv_data_params_t * params)
{
    if (volumeRenderer_CanAdvertise(params))
    {
        return VOLUME_CONTROL_SERVER_NUMBER_ADVERT_DATA_ITEMS;
    }
    return 0;
}

static le_adv_data_item_t volumeRenderer_GetAdvertisingData(const le_adv_data_params_t * params, unsigned int number)
{
    UNUSED(number);
    le_adv_data_item_t item = { .size = 0, .data = NULL };
    if (volumeRenderer_CanAdvertise(params))
    {
        DEBUG_LOG("volumeRenderer_GetAdvertisingData data_set: %d, completeness: %d, placement:%d size:%d",
               params->data_set, params->completeness, params->placement, volume_renderer_advert.size);

        volumeRenderer_GetItemData(&item);
        return item;;
    }

    Panic();
    return item;
}

static void volumeRenderer_ReleaseAdvertisingData(const le_adv_data_params_t * params)
{
    UNUSED(params);
    return;
}

le_adv_data_callback_t volumeRenderer_AdvertisingManagerCallback =
{
    .GetNumberOfItems = &volumeRenderer_GetNumberOfAdvertisingItems,
    .GetItem = &volumeRenderer_GetAdvertisingData,
    .ReleaseItems = &volumeRenderer_ReleaseAdvertisingData
};
#endif

bool VolumeRenderer_SetupLeAdvertisingData(void)
{
#ifndef INCLUDE_LEGACY_LE_ADVERTISING_MANAGER
    volume_renderer_registered_handle = LeAdvertisingManager_RegisterAdvertisingItemCallback(NULL, &volumeRenderer_AdvertisingManagerCallback);
#else
    volume_renderer_registered_handle = LeAdvertisingManager_Register(NULL, &volumeRenderer_AdvertisingManagerCallback);
#endif
    volume_renderer_advert.size = VOLUME_CONTROL_SERVER_SIZE_ADVERT;
    volume_renderer_advert.data = volume_renderer_data;
    return (volume_renderer_registered_handle ? TRUE : FALSE);
}

bool VolumeRenderer_UpdateLeAdvertisingData(void)
{
#ifndef INCLUDE_LEGACY_LE_ADVERTISING_MANAGER
    return (volume_renderer_registered_handle ? LeAdvertisingManager_UpdateAdvertisingItem(volume_renderer_registered_handle) : FALSE);
#else
    return (volume_renderer_registered_handle ? LeAdvertisingManager_NotifyDataChange(NULL, volume_renderer_registered_handle) : FALSE);
#endif
}


